#====# Appendix J: Robustness to alternative event analysis tests #====#

# Load libraries and set defaults ----
library(fixest)
library(estudy2)
library(bizdays)
library(modelsummary)
library(tinytable)
library(tidyverse)
library(tidylog, warn.conflicts = FALSE)

# prepare business calendar:
business_calendar <- create.calendar('biz_calendar', weekdays = c('saturday','sunday'))

# Import data ----
stocks <- read_rds("data_out/stocks_analysis.rds") # main analysis dataset

# Table J.1: Linear models of AR ----
dat <- stocks %>%
  filter(date >= offset(event, -5, business_calendar) &
           date <= offset(event, +5, business_calendar)) %>%
  mutate(event_day = as.numeric(date == event))

mod_ar1 <- feols(abn_chg ~ event_day,
                 dat %>%
                   filter(FCPA_sample == 1),
                 cluster = ~gvkey)

mod_ar_FE1 <- feols(abn_chg ~ event_day | gvkey,
                    dat %>%
                      filter(FCPA_sample == 1),
                    cluster = ~gvkey)

mod_ar_ind1 <- feols(abn_chg ~ event_day | naics2,
                     dat %>%
                       filter(FCPA_sample == 1),
                     cluster = ~gvkey)

mod_ar0 <- feols(abn_chg ~ event_day,
                 dat %>%
                   filter(FCPA_sample == 0),
                 cluster = ~gvkey)

mod_ar_FE0 <- feols(abn_chg ~ event_day | gvkey,
                    dat %>%
                      filter(FCPA_sample == 0),
                    cluster = ~gvkey)

mod_ar_ind0 <- feols(abn_chg ~ event_day | naics2,
                     dat %>%
                       filter(FCPA_sample == 0),
                     cluster = ~gvkey)

rows <- tibble::tribble(
  ~term, ~mod_ar1, ~mod_ar_FE1, ~mod_ar_ind1, ~mod_ar0, ~mod_ar_FE0, ~mod_ar_ind0,
  "Firm-FE", "", "Yes", "", "", "Yes", "", 
  "Industry-FE", "", "", "Yes", "", "", "Yes",
  "Std.Errors", "by: firm", "by: firm", "by: firm", "by: firm", "by: firm", "by: firm")
attr(rows, "position") <- c(5,6,7)

modelsummary(list("(1)" = mod_ar1, 
                  "(2)" = mod_ar_FE1, 
                  "(3)" = mod_ar_ind1, 
                  "(4)" = mod_ar0,
                  "(5)" = mod_ar_FE0,
                  "(6)" = mod_ar_ind0),
             stars = c("*" = 0.05),
             escape = FALSE,
             coef_map = c("event_day" = "Executive Order",
                          "FCPA_sample" = "Past FCPA target",
                          "event_day:FCPA_sample" = "Executive Order $\\times$ Past FCPA target",
                          "(Intercept)" = "(Intercept)"),
             gof_omit = "R2 Within|AIC|BIC|RMSE|FE|Std.Errors",
             notes = paste0("Linear models of \\textsc{ar} to past FCPA targets and matched placebo firms ",
                            "per day. Standard errors clustered by firm in parentheses. ",
                            "Estimation window starts 30 days and ends 5 days before FCPA Executive Order. Market models estimated using ",
                            "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                            collapse = ""),
             title = "Linear models of \\textsc{ar} \\label{tab:rob_linear}",
             add_rows = rows) %>%
  group_tt(j = list("Past FCPA targets" = 2:4,
                    "Non-FCPA targets" = 5:7)) %>%
  style_tt(i = 7, line_color = "white", line_width = 0.005, line = "t") %>%
  style_tt(i = 5, line_color = "black", line_width = 0.05, line = "t") %>%
  theme_tt("resize", width = .9) %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_J1.html", overwrite = TRUE)

# Table J.2: Parametric tests, past FCPA targets ----
list_of_returns <- map(.x = unique(stocks$ticker_symbol[stocks$FCPA_sample == 1 & !is.na(stocks$abn_chg)]),
                       .f = ~returns(rates = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, obs_chg) %>%
                                       as.data.frame(),
                                     regressor = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, fit_chg) %>%
                                       as.data.frame(),
                                     estimation_start = offset(as.Date("2025-02-10"), n = -30, business_calendar),
                                     estimation_end = offset(as.Date("2025-02-10"), n = -5, business_calendar),
                                     market_model = "sim"))

parametric_tests(list_of_returns, 
                 event_start = offset("2025-02-10", n = -4, business_calendar),
                 event_end = offset("2025-02-10", n = +5, business_calendar)) %>%
  as_tibble() %>%
  mutate(across(.cols = matches("signif"),
                .fns = ~case_when(.x %in% c("**", "***") ~ "*",
                                  TRUE ~ ""))) %>%
  mutate(date = format(date, format = "%a, %b %d %Y"),
         mean = sprintf("%.3f", mean),
         bw_1980_stat = paste0(sprintf("%.3f", bw_1980_stat), bw_1980_signif),
         bw_1985_stat = paste0(sprintf("%.3f", bw_1985_stat), bw_1985_signif),
         t_test_stat = paste0(sprintf("%.3f", t_test_stat), t_test_signif),
         pt_stat = paste0(sprintf("%.3f", pt_stat), pt_signif),
         bh_stat = paste0(sprintf("%.3f", bh_stat), bh_signif),
         lmb_stat = paste0(sprintf("%.3f", lmb_stat), lmb_signif)) %>%
  select(date, mean, bw_1980_stat, bw_1985_stat, t_test_stat, pt_stat, bh_stat, lmb_stat) %>%
  rename("Date" = "date",
         "Estimate" = "mean",
         "BW 1980" = "bw_1980_stat",
         "BW 1985" = "bw_1985_stat",
         "T-test" = "t_test_stat",
         "Patell (1976)" = "pt_stat",
         "BMP 1991" = "bh_stat",
         "Lamb (1995)" = "lmb_stat") %>%
  datasummary_df(note = paste0("* p < 0.05 \\\\ Parametric event test results respectively from Brown and Warner \\citeyearpar[][BW 1980]{Brown_Warner1980}, ",
                               "Brown and Warner \\citeyearpar[][BW 1985]{Brown_Warner1985}, regular t-test, Patell \\citeyearpar{Patell1976}, ",
                               "Boehmer, Masumeci, and Poulsen \\citeyearpar[][BMP 1991]{Bohemer_etal1991}, and Lamb \\citeyearpar{Lamb1995}. ",
                               "Estimation window starts 30 days and ends 5 days before the Executive Order. Market models estimated using ",
                               "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                               collapse = ""),
                 escape = TRUE,
                 title = "Parametric tests, past FCPA targets \\label{tab:param_1}") %>%
  group_tt(i = list("Pre-event:" = 1,
                    "Post-event:" = 5)) %>%
  theme_tt("resize", width = .9) %>% 
  style_tt(j = 2:8, align = "c") %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_J2.html", overwrite = TRUE)

# Table J.3: Parametric tests, non-FCPA targets ----
list_of_returns <- map(.x = unique(stocks$ticker_symbol[stocks$FCPA_sample == 0 & !is.na(stocks$abn_chg) &
                                                          stocks$fit_R2 >= 0.01]),
                       .f = ~returns(rates = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, obs_chg) %>%
                                       as.data.frame(),
                                     regressor = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, fit_chg) %>%
                                       as.data.frame(),
                                     estimation_start = offset(as.Date("2025-02-10"), n = -30, business_calendar),
                                     estimation_end = offset(as.Date("2025-02-10"), n = -5, business_calendar),
                                     market_model = "sim"))

parametric_tests(list_of_returns, 
                 event_start = offset("2025-02-10", n = -4, business_calendar),
                 event_end = offset("2025-02-10", n = +5, business_calendar)) %>%
  as_tibble() %>%
  mutate(across(.cols = matches("signif"),
                .fns = ~case_when(.x %in% c("**", "***") ~ "*",
                                  TRUE ~ ""))) %>%
  mutate(date = format(date, format = "%a, %b %d %Y"),
         mean = sprintf("%.3f", mean),
         bw_1980_stat = paste0(sprintf("%.3f", bw_1980_stat), bw_1980_signif),
         bw_1985_stat = paste0(sprintf("%.3f", bw_1985_stat), bw_1985_signif),
         t_test_stat = paste0(sprintf("%.3f", t_test_stat), t_test_signif),
         pt_stat = paste0(sprintf("%.3f", pt_stat), pt_signif),
         bh_stat = paste0(sprintf("%.3f", bh_stat), bh_signif),
         lmb_stat = paste0(sprintf("%.3f", lmb_stat), lmb_signif)) %>%
  select(date, mean, bw_1980_stat, bw_1985_stat, t_test_stat, pt_stat, bh_stat, lmb_stat) %>%
  rename("Date" = "date",
         "Estimate" = "mean",
         "BW 1980" = "bw_1980_stat",
         "BW 1985" = "bw_1985_stat",
         "T-test" = "t_test_stat",
         "Patell (1976)" = "pt_stat",
         "BMP 1991" = "bh_stat",
         "Lamb (1995)" = "lmb_stat") %>%
  datasummary_df(note = paste0("* p < 0.05 \\\\ Parametric event test results respectively from Brown and Warner \\citeyearpar[][BW 1980]{Brown_Warner1980}, ",
                               "Brown and Warner \\citeyearpar[][BW 1985]{Brown_Warner1985}, regular t-test, Patell \\citeyearpar{Patell1976}, ",
                               "Boehmer, Masumeci, and Poulsen \\citeyearpar[][BMP 1991]{Bohemer_etal1991}, and Lamb \\citeyearpar{Lamb1995}. ",
                               "Estimation window starts 30 days and ends 5 days before the Executive Order. Market models estimated using ",
                               "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                               collapse = ""),
                 escape = TRUE,
                 title = "Parametric tests, non-FCPA targets \\label{tab:param_0}") %>%
  group_tt(i = list("Pre-event:" = 1,
                    "Post-event:" = 5)) %>%
  theme_tt("resize", width = .9) %>% 
  style_tt(j = 2:8, align = "c") %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_J3.html", overwrite = TRUE)

# Table J.4: Non-parametric tests, past FCPA targets ----
list_of_returns <- map(.x = unique(stocks$ticker_symbol[stocks$FCPA_sample == 1 & !is.na(stocks$abn_chg)]),
                       .f = ~returns(rates = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, obs_chg) %>%
                                       as.data.frame(),
                                     regressor = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, fit_chg) %>%
                                       as.data.frame(),
                                     estimation_start = offset(as.Date("2025-02-10"), n = -30, business_calendar),
                                     estimation_end = offset(as.Date("2025-02-10"), n = -5, business_calendar),
                                     market_model = "sim"))

nonparametric_tests(list_of_returns, 
                    event_start = offset("2025-02-10", n = -4, business_calendar),
                    event_end = offset("2025-02-10", n = +5, business_calendar)) %>%
  as_tibble() %>%
  mutate(across(.cols = matches("signif"),
                .fns = ~case_when(.x %in% c("**", "***") ~ "*",
                                  TRUE ~ ""))) %>%
  mutate(date = format(date, format = "%a, %b %d %Y"),
         sign_stat = paste0(sprintf("%.3f", sign_stat), sign_signif),
         gsign_stat = paste0(sprintf("%.3f", gsign_stat), gsign_signif),
         csign_stat = paste0(sprintf("%.3f", csign_stat), csign_signif),
         rank_stat = paste0(sprintf("%.3f", rank_stat), rank_signif),
         mrank_stat = paste0(sprintf("%.3f", mrank_stat), mrank_signif),
         wlcx_stat = paste0(sprintf("%.3f", wlcx_stat), wlcx_signif)) %>%
  select(-weekday, -percentage, -matches("signif")) %>%
  rename("Date" = "date",
         "Sign test" = "sign_stat",
         "Gen. sign test" = "gsign_stat",
         "Corrado sign test" = "csign_stat",
         "Rank test" = "rank_stat",
         "Mod. rank test" = "mrank_stat",
         "Wilcoxon test" = "wlcx_stat") %>%
  datasummary_df(note = paste0("* p < 0.05 \\\\ Non-parametric event test results respectively from a sign test \\citep{Bohemer_etal1991}, ",
                               "a generalized sign test \\citep{McConnell_Muscarella1985}, a Corrado sign test \\citep{Corrado_Zivney1992}, ",
                               "a rank test \\citep{Cowan1992}, a modified rank test \\citep{Corrado_Zivney1992}, and a Wilcoxon test \\citep{Wilcoxon1992}. ",
                               "Estimation window starts 30 days and ends 5 days before the Executive Order. Market models estimated using ",
                               "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                               collapse = ""),
                 escape = TRUE,
                 title = "Non-parametric tests, past FCPA targets \\label{tab:nonparam_1}") %>%
  group_tt(i = list("Pre-sanctions:" = 1,
                    "Post-sanctions:" = 5)) %>%
  theme_tt("resize", width = .9) %>%
  style_tt(j = 2:7, align = "c") %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_J4.html", overwrite = TRUE)

# Table J.5: Non-parametric tests, non-FCPA targets ----
list_of_returns <- map(.x = unique(stocks$ticker_symbol[stocks$FCPA_sample == 0 & !is.na(stocks$abn_chg)]),
                       .f = ~returns(rates = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, obs_chg) %>%
                                       as.data.frame(),
                                     regressor = stocks %>%
                                       dplyr::filter(ticker_symbol == .x) %>%
                                       dplyr::select(date, fit_chg) %>%
                                       as.data.frame(),
                                     estimation_start = offset(as.Date("2025-02-10"), n = -30, business_calendar),
                                     estimation_end = offset(as.Date("2025-02-10"), n = -5, business_calendar),
                                     market_model = "sim"))

nonparametric_tests(list_of_returns, 
                    event_start = offset("2025-02-10", n = -4, business_calendar),
                    event_end = offset("2025-02-10", n = +5, business_calendar)) %>%
  as_tibble() %>%
  mutate(across(.cols = matches("signif"),
                .fns = ~case_when(.x %in% c("**", "***") ~ "*",
                                  TRUE ~ ""))) %>%
  mutate(date = format(date, format = "%a, %b %d %Y"),
         sign_stat = paste0(sprintf("%.3f", sign_stat), sign_signif),
         gsign_stat = paste0(sprintf("%.3f", gsign_stat), gsign_signif),
         csign_stat = paste0(sprintf("%.3f", csign_stat), csign_signif),
         rank_stat = paste0(sprintf("%.3f", rank_stat), rank_signif),
         mrank_stat = paste0(sprintf("%.3f", mrank_stat), mrank_signif),
         wlcx_stat = paste0(sprintf("%.3f", wlcx_stat), wlcx_signif)) %>%
  select(-weekday, -percentage, -matches("signif")) %>%
  rename("Date" = "date",
         "Sign test" = "sign_stat",
         "Gen. sign test" = "gsign_stat",
         "Corrado sign test" = "csign_stat",
         "Rank test" = "rank_stat",
         "Mod. rank test" = "mrank_stat",
         "Wilcoxon test" = "wlcx_stat") %>%
  datasummary_df(note = paste0("* p < 0.05 \\\\ Non-parametric event test results respectively from a sign test \\citep{Bohemer_etal1991}, ",
                               "a generalized sign test \\citep{McConnell_Muscarella1985}, a Corrado sign test \\citep{Corrado_Zivney1992}, ",
                               "a rank test \\citep{Cowan1992}, a modified rank test \\citep{Corrado_Zivney1992}, and a Wilcoxon test \\citep{Wilcoxon1992}. ",
                               "Estimation window starts 30 days and ends 5 days before the Executive Order. Market models estimated using ",
                               "the LASSO and individual S\\&P 500 constituents as predictors, selected using 15-fold cross validation. ",
                               collapse = ""),
                 escape = TRUE,
                 title = "Non-parametric tests, non-FCPA targets \\label{tab:nonparam_0}") %>%
  group_tt(i = list("Pre-sanctions:" = 1,
                    "Post-sanctions:" = 5)) %>%
  theme_tt("resize", width = .9) %>%
  style_tt(j = 2:7, align = "c") %>%
  theme_tt("placement", latex_float = "!htbp") %>%
  save_tt("tables/table_J5.html", overwrite = TRUE)

#====# The End #====#